package com.android.cookbook.listener;

import java.io.IOException;

import com.android.cookbook.activity.ListDriver;
import com.android.cookbook.db.BaseDatosHelper;
import com.android.cookbook.util.Constant;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;

public class RecipeCreateListener implements OnClickListener{
	
	BaseDatosHelper myDBHelper;
	String strTypeCreate;
	String edit_name;
	String edit_description;
	String edit_ingredients;
	String edit_direction;
	int     recipeId;
	Context context;
	
	public RecipeCreateListener(Context context,
								String  strTypeCreate, 
								String  edit_name, 
								String  edit_description, 
								String  edit_ingredients,
								String  edit_direction,
								int     recipeId){
		
		this.context = context;
		this.strTypeCreate = strTypeCreate;
		this.edit_name = edit_name;
		this.edit_description = edit_description;
		this.edit_ingredients = edit_ingredients;
		this.edit_direction = edit_direction;
		this.recipeId = recipeId;
		
	}
	public void onClick(View view) {    
		createDB();
		if ("EDIT".equals(strTypeCreate)){
			editRecipe();
		}else{
			createRecipe();
		}
		Bundle bundle = new Bundle();                 
		bundle.putString(Constant.TYPE_ID,Constant.TYPE_MY_RECIPE);                   
		bundle.putString(Constant.TYPE_SEARCH,Constant.LIST_BY_ID);   

		
		Intent myIntent = new Intent(view.getContext(),ListDriver.class);
		myIntent.putExtras(bundle);                   
		context.startActivity(myIntent); 
		((Activity)context).finish();
		
	} 

	public void createDB() {         
		myDBHelper = new BaseDatosHelper(context);         
		try {             
			myDBHelper.createDataBase();         
		} catch (IOException ioe) {             
			throw new Error("Unable to create database");         
		}     
	}  
	
	public void createRecipe() {         
		myDBHelper.openDataBase();       
		System.out.println("edit_name:"+ edit_name);
		System.out.println("edit_description:"+ edit_description);
		System.out.println("edit_ingredients:"+ edit_ingredients);
		System.out.println("edit_direction:"+ edit_direction);
		myDBHelper.createRecipe("hola",
				  				edit_description,
				  				edit_ingredients,
				  				edit_direction);       
		myDBHelper.close();         
		return;     
	}

	public void editRecipe() {         
		myDBHelper.openDataBase();         
		myDBHelper.updateRecipe(recipeId,
								edit_name,
								edit_description,
								edit_ingredients,
								edit_direction);       
		myDBHelper.close();         
		return;     
	}
}
